package ru.kantiana.accounting.rldata.rl.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: Pavel Belevich
 * Date: 3/27/11
 * Time: 4:33 PM
 */
public class RLParserImpl implements RLParser {

    private static final String RL_PATTERN_STRING = ":========================== =VV= =====::========================== =VV= =====:\\s*:\\s*(\\S+)\\s+(.+?)\\s*:.+?(@|\\Z)";


    private static final Pattern RL_PATTERN = Pattern.compile(RL_PATTERN_STRING, Pattern.DOTALL);

    public Collection<RL> parse(CharSequence charSequence) {

        Collection<RL> rls = new ArrayList<RL>();

        Matcher matcher = RL_PATTERN.matcher(charSequence);

        while (matcher.find()) {
            RL rl = new RL();
            rl.setText(matcher.group(0));
            rl.setNumber(matcher.group(1));
            rl.setName(matcher.group(2));
            rls.add(rl);
        }

        return rls;
    }

}
